package com.zjdiepu.www.biz.zx;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.zpp.PublicDao;
import com.zjdiepu.www.dao.zx.ZxCreditQueryMapper;
import com.zjdiepu.www.dao.zx.ZxQueryCreditApplyMapper;
import com.zjdiepu.www.dao.zx.ZxQueryCreditApplySubMapper;
import com.zjdiepu.www.ibiz.base.ISerialNumberService;
import com.zjdiepu.www.ibiz.zx.IZxQueryCreditApplyService;
import com.zjdiepu.www.model.zx.dto.ZxQueryCreditApply;
import com.zjdiepu.www.model.zx.ext.ZxQueryCreditApplyExt;

/**
 * zx_征信查询申请Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年05月09日 15时16分<br/>
 * @since 1.0<br/>
 */
@Service
public class ZxQueryCreditApplyServiceImpl extends BaseService implements IZxQueryCreditApplyService {
	public static final Logger logger = LoggerFactory.getLogger(ZxQueryCreditApplyServiceImpl.class);

	@Autowired
	private ZxQueryCreditApplyMapper zxQueryCreditApplyMapper;
	@Autowired
	private ZxQueryCreditApplySubMapper zxQueryCreditApplySubMapper;
	@Autowired
	private ZxCreditQueryMapper zxCreditQueryMapper;
	@Autowired
	private ISerialNumberService serialNumberService;
	@Autowired
	private PublicDao publicDao;
	
	
	@Override
	public void saveUpdateAndDelete(ZxQueryCreditApply zxQueryCreditApply, OperateType operType) {
		switch (operType) {
		case add:
			zxQueryCreditApply.initInsertData(zxQueryCreditApply.getModifier(), zxQueryCreditApply.getModifierName());
			zxQueryCreditApplyMapper.insert(zxQueryCreditApply);
			break;
		case edit:
			zxQueryCreditApply.initUpdateData(zxQueryCreditApply.getModifier(), zxQueryCreditApply.getModifierName());
			// 影响的记录数
			int updateCount = zxQueryCreditApplyMapper.updateByPrimaryKeySelective(zxQueryCreditApply);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			zxQueryCreditApply.initDeleteData(zxQueryCreditApply.getModifier(), zxQueryCreditApply.getModifierName());
			// 影响的记录数
			int deleteCount = zxQueryCreditApplyMapper.updateByPrimaryKeySelective(zxQueryCreditApply);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ZxQueryCreditApply> selectByExt(ZxQueryCreditApplyExt zxQueryCreditApplyExt) {
		return zxQueryCreditApplyMapper.selectByExt(zxQueryCreditApplyExt);
	}
	
	@Override
	public int countByExt(ZxQueryCreditApplyExt zxQueryCreditApplyExt){
		return zxQueryCreditApplyMapper.countByExt(zxQueryCreditApplyExt);
	}
	
	@Override
	public ZxQueryCreditApply selectById(String id) {
		return zxQueryCreditApplyMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			zxQueryCreditApplyMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ZxQueryCreditApply zxQueryCreditApply) {
		return zxQueryCreditApplyMapper.countRefined(zxQueryCreditApply);
	}

	@Override
	public ZxQueryCreditApply selectRefined(ZxQueryCreditApply zxQueryCreditApply) {
		return zxQueryCreditApplyMapper.selectRefined(zxQueryCreditApply);
	}
	
	@Override
	public int deletedRefined(ZxQueryCreditApply zxQueryCreditApply) {
		return zxQueryCreditApplyMapper.deletedRefined(zxQueryCreditApply);
	}

	@Override
	public boolean repeat(ZxQueryCreditApply zxQueryCreditApply) {
		int count = countRefined(zxQueryCreditApply);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(zxQueryCreditApply.getId())) {
			return true;
		}
		ZxQueryCreditApply dbItem = selectRefined(zxQueryCreditApply);
		if(null == dbItem) {
			return false;
		}
		return !zxQueryCreditApply.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return zxQueryCreditApplyMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<ZxQueryCreditApply> zxQueryCreditApplys) {
		return zxQueryCreditApplyMapper.insertBatch(zxQueryCreditApplys);
	}
}
